package com.springcloud.controller;

import com.springcloud.dto.Response;

import com.springcloud.model.DestinationModel;
import com.springcloud.serviceex.DispatchService;
import com.springcloud.utils.HttpClientHelper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
public class MoveController {

    DispatchService dispatchService;
    @Autowired
    public void setDispatchService(DispatchService dispatchService) {
        this.dispatchService = dispatchService;

    }
    @PostMapping("/rest/chargingPile")
    public Response<String> chargingPile(){
        Response<String> dto=new  Response<>();
        ChargingPosModel pos=getPos();
        //判断当前的位置
        double offsetX=pos.getRefX()-6;
        double offsetY=pos.getRefY()-2.5;
        if (abs(offsetX)<0.05&&abs(offsetY)<0.05){

            dto.setMessage("当前已在充电位置");
            return  dto;
        }
        move2Pile("0113");
        dto.success();
        return dto;
    }
    @GetMapping("/rest/getNumber")
    public Response<String> getNumber(){
        Response<String> dto=new  Response<>();
        String power = dispatchService.getPower();
        if(power==null||"".equals(power)){
            dto.fail();
            return dto;
        }
        dto.success(power);
        return dto;
    }
    public ChargingPosModel getPos(){
        ChargingPosModel pos=new ChargingPosModel();
        String refX= dispatchService.getRefX();
        String refY= dispatchService.getRefY();
        pos.setRefX(Double.parseDouble(refX));
        pos.setRefY(Double.parseDouble(refY));
        return pos;
    }
    public static double abs(double a) {
        return (a < 0) ? -a : a;
    }
    public static void move2Pile(String location) {
        List<DestinationModel> list=new ArrayList<>();
        Random random=new Random();
        int i=random.nextInt(1000);
        String taskNo=i+"";
        DestinationModel model=new DestinationModel();
        model.setOperation("13");
        model.setLocationName(location);
        list.add(model);
        HttpClientHelper.createOrder(taskNo, list);
    }
    @Data
    public static class ChargingPosModel{
        private double refX;
        private double refY;

    }

}
